找传奇、传世资源到传世资源站!

RabbitMQ案例(入门级)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboardusing RabbitMQ.Client;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication2{ /// <summary> /// -------------- /// 生产者 /// -------------- /// </summary> class Program { const string ExchangeName = "test.exchange"; const string QueueName = "test.queue"; /// <summary> /// 生产者 /// </summary> /// <param name="args"></param> static void Main(string[] args) { ConnectionFactory rabbitMqFactory = new ConnectionFactory { HostName = "localhost", UserName = "test01", Password = "test01", VirtualHost = "VH" }; using (IConnection conn = rabbitMqFactory.CreateConnection()) using (IModel channel = conn.CreateModel()) { //声明消息队列,且为可持久化的 channel.ExchangeDeclare(ExchangeName, "direct", durable: true, autoDelete: false, arguments: null); //声明消息队列,且为可持久化的 channel.QueueDeclare(QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null); //绑定队列 channel.QueueBind(QueueName, ExchangeName, routingKey: QueueName); //将队列设置为持久化之后,还需要将消息也设为可持久化的 var props = channel.CreateBasicProperties(); // props.SetPersistent(true); props.DeliveryMode = 2; for (int i = 0; i < 18000; i ) { // System.Threading.Thread.Sleep(5000); props.Priority = (i == 5) ? (byte)10 : (byte)i; string message = "测试测试" i.ToString(); var body = Encoding.UTF8.GetBytes(message); //发布信息 channel.BasicPublish(exchange: ExchangeName, routingKey: QueueName, basicProperties: props, body: body); Console.WriteLine("Program Sent {0}", message); } channel.Close(); conn.Close(); } Console.WriteLine("OK-生产者..............."); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复